package com.yunze.apiCommon.upstreamAPI.YiChe;

import com.yunze.apiCommon.utils.MD5Util;
import org.apache.commons.codec.digest.DigestUtils;

import java.util.*;

/**
 * 易彻物联网平台 API 接口
 */
public class YiChe_Api {


	//构造赋值
	public YiChe_Api(Map<String, Object> init_map){
		appkey = init_map.get("cd_username").toString();
		String cd_pwd[] = init_map.get("cd_pwd").toString().split(",");
		secret = cd_pwd[0];
		operator = cd_pwd[1];
		server_Ip = init_map.get("cd_key").toString()+"/api/v2";

		params = new HashMap<>();//初始化请求数据

		long timestamp = System.currentTimeMillis() / 1000L; // 将毫秒数除以1000并向下取整
		params.put("appkey",appkey);
		params.put("timestamp",timestamp);
		Random random = new Random();
		int nonce = random.nextInt(90000) + 10000;
		params.put("nonce", nonce);

	}

	// 服务器请求地址
	protected  static String server_Ip = "";
	// 代理编号
	public static String appkey = null;
	// 秘钥 secret
	public static String secret = "";

	public static String operator = "";//运营商

	public static Map<String, Object> params = null;

	// 配置调用的名称
	protected static String Config_name = "";



	public static String generateSign(Map<String, Object> paramMap) {
		List<String> list = new ArrayList<>(20);
		for (Map.Entry<String, Object> entry : paramMap.entrySet()) {
			list.add(entry.getKey() + "=" + entry.getValue() + "&");
		}
		Collections.sort(list);
		String signParam = String.join("", list);
		String param = signParam + "secret=" + secret;
		return DigestUtils.sha1Hex(param).toUpperCase();
	}
}
